package com.mz.ozy.activity.assets;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.UsdtDetailAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.UsdtDetailBean;
import com.mz.ozy.mvp.presenter.UsdtDetailPresenter;
import com.mz.ozy.mvp.view.UsdtDetailContract;
import com.mz.ozy.utils.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * kt明细
 */
public class QcjfDetailsActivity extends BaseActivity<UsdtDetailContract.View, UsdtDetailPresenter> implements UsdtDetailContract.View {

    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_exchange)
    TextView tvExchange;
    private int pageNo = 1;
    private int tag = 0;
    private List<UsdtDetailBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private UsdtDetailBean usdtDetailBean;
    private UsdtDetailAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qcjf_details);
        ButterKnife.bind(this);
        initTitle2();
        tvCenterTitle.setText(R.string.str_qi_quan_price);
        tvBack.setImageResource(R.mipmap.white_back_icon);
        tvCenterTitle.setTextColor(getResources().getColor(R.color.white));
        llNoData.setVisibility(View.GONE);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new UsdtDetailAdapter(this, mBeanList);
        recyclerview.setAdapter(adapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getUsdtDetail(QcjfDetailsActivity.this, String.valueOf(4), String.valueOf(pageNo), false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshList(false);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshList(true);
    }

    private void refreshList(boolean b) {
        if (mPresenter == null) return;
        tag = 0;
        pageNo = 1;
        mPresenter.getUsdtDetail(QcjfDetailsActivity.this, String.valueOf(4), String.valueOf(pageNo), b);
    }

    @Override
    public void getUsdtDetailSuccess(UsdtDetailBean bean) {
        usdtDetailBean = bean;
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        tvBalance.setText(bean.getBalance());
        mBeanList.addAll(bean.getPage().getPageItems());
        adapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @OnClick(R.id.tv_exchange)
    public void onViewClicked() {
        if (usdtDetailBean != null) {
            if (usdtDetailBean.getExchangeopen() == 1) {


            } else {
                ToastUtils.showToast("兑换功能未开放");
            }
        }
    }
}
